package com.sf.test.web.controller;  
  
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sf.test.web.model.User;
import com.sf.test.web.service.IUserService;

  
@Controller  
@RequestMapping("/user")  
public class UserController {  
    @Resource  
    private IUserService userService;  
      
    @RequestMapping(value="/showUser", method = RequestMethod.GET)  
    public String getUserById(HttpServletRequest request,Model model){  
        String id = request.getParameter("id");  
        User user = this.userService.getUserById(id);  
        model.addAttribute("user", user);  
        return "user";  
    }  
    
    @RequestMapping(value="/getUser", method = RequestMethod.GET)  
    @ResponseBody
    public List<User> getUser(HttpServletRequest request){  
        String userId = request.getParameter("userId");  
        List<User> allUser =new ArrayList<User>();
        if(StringUtils.isEmpty(userId)){
        	allUser = this.userService.getAllUser();  
        }else{
        User user = this.userService.getUserById(userId);  
        allUser.add(user);
        }
        return allUser;  
    }  
    
    @RequestMapping(value="/addUser", method = RequestMethod.POST,produces="text/html;charset=UTF-8")  
    @ResponseBody
    public String addUser(@RequestBody User user){  
        this.userService.addUser(user);
		return "用户"+user.getUserId()+"新增成功!";  
    }  
    
    @RequestMapping(value="/modifyUser", method = RequestMethod.POST,produces="text/html;charset=UTF-8")  
    @ResponseBody
    public String modifyUser(@RequestBody User user){  
    	this.userService.modifyUser(user);  
    	return "用户"+user.getUserId()+"修改成功!";  
    }  
    @RequestMapping(value="/deleteUser",method = RequestMethod.GET,produces="text/html;charset=UTF-8")  
    @ResponseBody
    public String deleteUser(HttpServletRequest request){ 
    	String userId = request.getParameter("userId");  
    	this.userService.deleteUser(userId);  
    	return "用户"+userId+"删除成功!";  
    }  
    
    @RequestMapping(value="/batchAddUser", method = RequestMethod.POST,produces="text/html;charset=UTF-8")  
    @ResponseBody
    public void batchAddUser(HttpServletRequest request){  
    	this.userService.batchAddUser(null);
    }  
}  